<?php
	require_once("../init.php");
	

	
	class SqlSeletor{
		
		var $select;
		var $fields;
		var $from;
		var $where;
		var $having;
		var $group_by;
		var $order_by;
		var $limit;
		var $sql;
		
		var $pieces;
		 
		var $cmd=array("SELECT","FROM","WHERE","HAVING","GROUP BY","ORDER BY","LIMIT");
		
		function SqlSeletor($query_str=null){
			if($query_str!=null){
				$this->parseStart($query_str);
				
				
				var_dump($this->pieces);
				/*$this->pieces=$sm;
				
				$this->select=array_map("trim",explode(",",$sm["SELECT"]));
				
				$this->from=explode(",",$sm["FROM"]);
				$this->where=$sm["WHERE"];
				$this->having=$sm["HAVING"];
				if(!empty($sm["GROUP BY"]))
					$this->group_by=explode(",",$sm["GROUP BY"]);
				if(!empty($sm["ORDER BY"])){
					$fields = explode(",",$sm["ORDER BY"]);
					$orders=array();
					foreach($fields as $field){
						if(!empty($field)){
							$f = explode(" ",$field);
							if(!ereg("ASC|DESC|asc|desc",$f[1])) $f[1]="ASC";
							$orders[$f[0]]=$f[1];
						}
					}
					$this->order_by=$orders;
				}
				if(!empty($sm["LIMIT"]))
					$this->limit=explode(",",$sm["LIMIT"]);*/
			}
		}
		
		
		
		function parseStart($query_str){
			$query_str = trim($query_str);
			$rs[]="^(SELECT) +([0-9a-zA-Z_,'\".()\* ]*) *";
			$rs[]="(FROM) +([0-9a-zA-Z_'\"\.]* *[0-9a-zA-Z_,'\"\.,]*) *";
			//LEFT +JOIN|INNER +JOIN|RIGHT +JOIN|
			$rs[]="(WHERE|GROUP|HAVING|ORDER|LIMIT) +(.*)";
			
			preg_match("/".implode("",$rs)."/i",$query_str,$pieces);
			$this->pieces["SELECT"] = trim($pieces[2]);
			$this->pieces["FROM"] = trim($pieces[4]);
			
			$this->pNext($pieces[5],$pieces[6]);
		}
		
		function pNext($cmd,$nextsql){
			$cmd=trim(ereg_replace(" +"," ",(strtoupper($cmd))));
			if($cmd=="LEFT JOIN") $this->parseNextLeftJoin($nextsql);
			if($cmd=="INNER JOIN") $this->parseNextInnerJoin($nextsql);
			if($cmd=="RIGHT JOIN") $this->parseNextRightJoin($nextsql);
			if($cmd=="WHERE") $this->parseNextWhere($nextsql);
			if($cmd=="HAVING") $this->parseNextHaving($nextsql);
			if($cmd=="GROUP") $this->parseNextGroupBy($nextsql);
			if($cmd=="ORDER") $this->paraseNextOrderBy($nextsql);
			if($cmd=="LIMIT") $this->parseNextLimit($nextsql);
		}
		
		function parseNextLeftJoin($nextsql){
			preg_match("/(.*) +(LEFT +JOIN|INNER +JOIN|RIGHT +JOIN|WHERE|GROUP|HAVING|ORDER|LIMIT) +(.*)/i",$nextsql,$pieces);
			var_dump($pieces);
			if($pieces==NULL) {
				$this->pieces["LEFT JOIN"][]=$nextsql;
			}else{
				$this->pieces["LEFT JOIN"][]=$pieces[1];
				
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		
		function parseNextInnerJoin($nextsql){
			preg_match("/(.*) +(LEFT|INNER|RIGHT|WHERE|GROUP|HAVING|ORDER|LIMIT) +(.*\))/i",$nextsql,$pieces);
			if($pieces==NULL) {
				$this->pieces["INNER JOIN"][]=$nextsql;
			}else{
				$this->pieces["INNER JOIN"][]=$pieces[1];
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		
		function parseNextRightJoin($nextsql){
			preg_match("/(.*) +(LEFT|INNER|RIGHT|WHERE|GROUP|HAVING|ORDER|LIMIT) +(.*\))/i",$nextsql,$pieces);
			if($pieces==NULL) {
				$this->pieces["RIGHT JOIN"][]=$nextsql;
			}else{
				$this->pieces["RIGHT JOIN"][]=$pieces[1];
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		
		function parseNextWhere($nextsql){
			preg_match("/(.*) +(GROUP|HAVING|ORDER|LIMIT) +(.*)/i",$nextsql,$pieces);
			if($pieces==NULL) {
				$this->pieces["WHERE"]=$nextsql;
			}else{
				$this->pieces["WHERE"]=$pieces[1];
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		
		function parseNextGroupBy($nextsql){
			preg_match("/(.*) +(HAVING|ORDER|LIMIT) +(.*)/i",$nextsql,$pieces);
			var_dump($nextsql);
			if($pieces==NULL) {
				$this->pieces["GROUP BY"]=ereg_replace("^BY ","",trim($nextsql));
			}else{
				$this->pieces["GROUP BY"]=ereg_replace("^BY ","",trim($pieces[1]));
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		
		function parseNextHaving($nextsql){
			preg_match("/(.*) +(ORDER|LIMIT) +(.*)/i",$nextsql,$pieces);
			var_dump($nextsql);
			if($pieces==NULL) {
				$this->pieces["ORDER BY"]=$nextsql;
			}else{
				$this->pieces["ORDER BY"]=$pieces[1];
				$this->pNext($pieces[2],$pieces[3]);
			}
		}
		

		
		function paraseNextOrderBy($nextsql){
			
			preg_match("/(.*) +(LIMIT) +(.*)/i",$nextsql,$pieces);
			if($pieces==NULL) {
				$this->pieces["ORDER BY"]=ereg_replace("^BY ","",trim($nextsql));
			}else{
				$this->pieces["ORDER BY"]=ereg_replace("^BY ","",trim($pieces[1]));
				$this->pNext($pieces[2],$pieces[3]);
			}
		}		
		
		function parseNextLimit($nextsql){
			$this->pieces["LIMIT"]=$nextsql;
		}
		
		
		
		function toString(){
			$sm = array();
			//$sm["SELECT"]=implode(",",$this->select);
			$select = $this->select;
			foreach($select as $i=>$s){
				if(ereg("^[a-zA-Z0-9_]*$",$s,$r)){
					$select[$i]=DB_PREFIX.$this->from[0].".".$s;
				}elseif(ereg("^[a-zA-Z0-9_]*.[a-zA-Z0-9_]*$",$s,$r)){
					$select[$i]=DB_PREFIX.$s;
				}
			}
			$sm["SELECT"]=implode(",",$select);
			$sm["FROM"]=implode(",",$this->from);
			$sm["WHERE"]=$this->where;
			$sm["HAVING"]=$this->having;
			$sm["GROUP BY"]=implode(",",$this->group_by);
			foreach(($this->order_by) as $k=>$v) $orders[]="{$k} {$v}";
			$sm["ORDER BY"]=implode(",",$orders);
			$sm["LIMIT"]=implode(",",$this->limit);
			
			$pieces=array();
			foreach($sm as $k=>$v) {
				if(isset($v))$pieces[]="$k $v";
			}
			return implode(" ",$pieces);
		}
		
		function select(){
			
		}
		
		function setFrom($tablename){
			
		}
		
		function setGroup(){
		
		}
		
		
		
		
		
		function setLimit($limit){
			var_dump($this->limit);
			if(count($this->limit)==1) $this->limit=array($limit);
		}
		
		function noGroup(){
		
		}
		
		function noOffset($offset){
			
		}
		
		function noLimit(){
			
		}
		
		
		
		
	}
	
	
	
	//
/*	$testsql="SELECT id, test, unit.create_at, unit.update_at, unit.trash_at, unit.is_trash FROM product_category,'asd where   sd', test   WHERE   'WHERE' = 1 AND ' ORDER by ' = 1 and  sdf = 'sdfsdf' ORDER BY sdf , asdsad";
	//LEFT JOIN t2 ON (column1) LEFT JOIN t2 ON (column1)
	$testsql = "SELECT college, region AS r, seed AS s, TEST('asdasd','asdad') FROM asdasd WHERE 1 ORDER BY r, s;";
	//$testsql = "SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL";
	$s = new SqlSeletor($testsql);
	$s->setLimit(2);
	echo $s->toString()*/
	
	

?>